<script lang="ts" setup>
import BaseButton from "@/components/BaseButton/index.vue";
import BaseUpDown from "@/components/BaseUpDown/index.vue";
import useSearch from "@/hooks/useSearch";
import useData from "./hooks/useData";
import { useUserStore } from "@/stores/modules/user";
const { pageData, roleMap } = useData();
import { deleteData, getDataByPage } from "@/api/modules/system/account";
import UpdateForm from "./components/UpdateForm/index.vue";
import UpdateFormPassword from "./components/UpdateFormPassword/index.vue";
const userStore = useUserStore();
const username = computed(() => userStore.userInfo.name);
const { tableData, loadData, formData , reloadData } = useSearch({
  defaultData: {},
  responseKey: {
    list: "result.data",
    total: "result.total"
  }
});

function loadDataCb() {
  loadData({
    api: getDataByPage,
    sendData: {
      pageNum: formData.pageNum - 1,
      pageSize: formData.pageSize
    }
  });
}

loadDataCb();
function reloadDataCb() {
  reloadData();
  loadData({
    api: getDataByPage,
    sendData: {
      pageNum: formData.pageNum - 1,
      pageSize: formData.pageSize
    }
  });
}

loadDataCb();

/**删除*/
import useDelete from "@/hooks/useDelete";
import { computed } from "vue";

const { deleteHandler, idList } = useDelete();

async function deleteCb(id) {
  await deleteHandler({
    api: deleteData,
    multiple: false,
    sendData: {
      id: [id]
    },
    hideMessageBox: false
  });
  loadDataCb();
}

function selectCb(data) {
  console.log(data);
  idList.value = data.rowList.map(item => item.userId);
}
</script>

<template>
  <BaseUpDown>
    <template #up>
      <div class="h-box justify-end">
        <UpdateForm @success="loadDataCb()">
          <BaseButton type="add_icon_text" />
        </UpdateForm>
        <BaseButton @click="reloadDataCb()" type="refresh_icon_text" />
      </div>
    </template>
    <he-table
      @handle-current-change="loadDataCb"
      :total="tableData.total"
      v-model:page-size="formData.pageSize"
      v-model:page-num="formData.pageNum"
      operate-width="180px"
      :operate-list="[]"
      :table-data="tableData.list"
      :column-data="pageData.columnData"
      :show-multiple="false"
      :show-summary="false"
      :show-operate="false"
      :show-multiple-column="false"
      :show-pagination="true"
      @select="selectCb"
    >
      <template #operationDefault="{ data }">
        <UpdateForm
          :row-data="{
            id: data.row[data.row.length - 1],
            name: data.row[1],
            role: roleMap.get(data.row[2])
          }"
          :title="`编辑用户:${data.row[1]}`"
          @success="loadDataCb"
        >
          <base-button type="edit_text"></base-button>
        </UpdateForm>
        <UpdateFormPassword
          :row-data="{
            id: data.row[data.row.length - 1],
            name: data.row[1],
            role: roleMap.get(data.row[2])
          }"
          :title="`重置密码:${data.row[1]}`"
          @success="loadDataCb"
        >
          <el-button type="primary" link>重置密码</el-button>
        </UpdateFormPassword>
        <base-button v-if="username!=data.row[1]" @click="deleteCb(data.row[data.row.length - 1])" type="del_text"></base-button>
      </template>
    </he-table>
  </BaseUpDown>
</template>
